package com.andy.mall.product.web;

import com.andy.mall.product.entity.CategoryEntity;
import com.andy.mall.product.service.CategoryService;
import com.andy.mall.product.vo.Category2Vo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class IndexController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 跳转到首页，并查出1级分类
     * @param model
     * @return
     */
    @GetMapping({"/","/index.html"})
    public String indexPage(Model model){
        List<CategoryEntity> list = categoryService.getLevel1Categorys();
        model.addAttribute("categorys",list);
        return "index";
    }

    /**
     * 查询二三级分类数据，组装成Json
     * @return
     */
    @ResponseBody
    @GetMapping("index/catalog.json")
    public Object getCategory2Json(){
        Map<String, List<Category2Vo>> map =categoryService.getCategory2Json();
        return map;
    }
}
